/* 
自定义hook函数: ajax请求
*/
import axios from 'axios'
import {ref} from 'vue'

export default function useUrlLoader<T>(url: string) {
  const result = ref<T|null>(null)
  const loading = ref(true)
  const errorMsg = ref(null)

  axios.get(url)
    .then(response => {
      result.value = response.data
      loading.value = false
    })
    .catch(error => {
      loading.value = false
      errorMsg.value = error.message
    })

  return {
    result,
    loading,
    errorMsg
  }
}